ubuntu22.04通过netplan配置网络

您所在的位置:网站首页 ubuntu test_io_manager ubuntu22.04通过netplan配置网络

ubuntu22.04通过netplan配置网络

2024-05-26 10:34| 来源: 网络整理| 查看: 265

1.以前的网络配置

ubuntu系统里通常在/etc/network/interfaces里配置好IP等信息 interfaces文件配置内容大概如下:

auto enp0s3 iface enp0s3 inet static address 10.0.2.15 netmask 255.255.255.0 gateway 10.0.2.1 dns-nameservers 218.85.157.99

保存关闭后,使用sudo systemctl restart networking或sudo /etc/init.d/networking restart重启网络,然后新的配置就会生效。

2.netplan工具

但是现在从ubuntu18.04开始,开始使用netplan工具来配置IP了,而不是使用interfaces文件和/etc/init.d/networking脚本。 如果执行以上2条命令,结果如下:

ubuntu@ubuntu:~$ sudo systemctl restart networking Failed to restart networking.service: Unit networking.service not found. ubuntu@ubuntu:~$ sudo /etc/init.d/networking restart [sudo] password for ubuntu: sudo: /etc/init.d/networking: command not found

查看ubuntu版本

ubuntu@ubuntu:~$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 22.04.3 LTS Release: 22.04 Codename: jammy

netplan工具默认已经安装

ubuntu@ubuntu:~$ netplan You need to specify a command usage: /usr/sbin/netplan [-h] [--debug] ... Network configuration in YAML options: -h, --help show this help message and exit --debug Enable debug messages Available commands: help Show this help message apply Apply current netplan config to running system generate Generate backend specific configuration files from /etc/netplan/*.yaml get Get a setting by specifying a nested key like "ethernets.eth0.addresses", or "all" info Show available features ip Retrieve IP information from the system set Add new setting by specifying a dotted key=value pair like ethernets.eth0.dhcp4=true rebind Rebind SR-IOV virtual functions of given physical functions to their driver try Try to apply a new netplan config to running system, with automatic rollback 2.1netplan的安装

题外话,由于我切换了apt源,并且更新了软件列表,安装了Python3-pip,导致netplan工具消失(具体安装过程我也没仔细看), 重新安装netplan.io报错: netplan.io : Depends: libnetplan0 (= 0.104-0ubuntu2~20.04.2) but 0.105-0ubuntu2~22.04.3 is to be installed 按照如下步骤重新安装即可:

apt install libudev1=245.4-4ubuntu3.22 apt install udev apt install network-manager apt install libnetplan0=0.104-0ubuntu2~20.04.2 apt install netplan.io 2.2配置静态IP

netplan工具通过.yaml配置文件对网卡进行配置,在/etc/netplan/目录下存在.yaml文件,可以先备份下原有文件再做修改,或者再新建一个yaml文件用于配置。 该文件只有root用户有write权限 -rw-r--r-- 1 root root 485 Aug 20 20:54 /etc/netplan/50-cloud-init.yaml 使用sudo命令 sudo nano /etc/netplan/50-cloud-init.yaml 修改文件内容,配置静态IP如下

network: ethernets: enp0s3: # 网卡名称 dhcp4: false # 使用静态IP match: macaddress: 52:54:00:a5:64:b5 set-name: enp0s3 addresses: [10.0.2.15/24] #gateway4: 10.0.2.1 `gateway4` has been deprecated routes: # 配置默认网关 - to: default via: 10.0.2.1 nameservers: # 配置dns服务器地址 addresses: [10.0.2.1, 218.85.157.99] version: 2 2.3测试配置

netplan try 该命令验证*.yaml配置文件是否有效

ubuntu@ubuntu:~$ netplan try /etc/netplan/50-cloud-init.yaml:13:13: Error in network definition: unknown key 'addressed' addressed: [10.0.2.15/24]

如上,配置文件有问题就会报错。正常可以看到

Press ENTER before the timeout to accept the new configuration Changes will revert in 103 seconds Configuration accepted.

配置通过验证了。

2.4应用新的配置

sudo netplan apply

2.5查看配置后的IP信息 ubuntu@ubuntu:~$ ip a 2: enp0s3: mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 52:54:00:a5:64:b5 brd ff:ff:ff:ff:ff:ff inet 10.0.2.15/24 metric 100 brd 10.0.2.255 scope global dynamic enp0s3 valid_lft 86399sec preferred_lft 86399sec inet6 fe80::5054:ff:fea5:64b5/64 scope link valid_lft forever preferred_lft forever 3.临时IP配置

如果网卡还没有配置IP,netplan工具也没有安装,apt install时是没有网络的,可以临时配置IP来解决

##添加临时IP ip addr add 10.0.2.15/24 dev enp0s3 ##添加网关 ip route add default via 10.0.2.1 ##up端口 ip link set eno3 up


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3